﻿Imports System.ComponentModel
Imports Pronama.Sekatyaku
Imports Pronama.Sekatyaku.Model

Namespace ViewModel

    Public Class VideoViewModel
        Inherits ViewModelBase

        Public Event DownloadInfoCompleted As EventHandler(Of EventArgs)
        Protected Sub OnDownloadInfoCompleted(ByVal e As EventArgs)
            RaiseEvent DownloadInfoCompleted(Me, e)
        End Sub

        Private Model As Video

        Public Property Id As String
            Get
                Return Model.Id
            End Get
            Set(ByVal value As String)
                Model.Id = value
                OnPropertyChanged("Id")
            End Set
        End Property

        Public Property Title As String
            Get
                Return Model.Title
            End Get
            Set(ByVal value As String)
                Model.Title = value
                OnPropertyChanged("Title")
            End Set
        End Property

        Public Property Description As String
            Get
                Return Model.Description
            End Get
            Set(ByVal value As String)
                If Model.Description = value Then
                    Exit Property
                End If
                Model.Description = value

                Dim d = value.Replace(vbLf, "").Replace(vbCr, "")
                _ShortDescription = If(d.Length > 40, d.Substring(0, 40), d)

                OnPropertyChanged("Description")
                OnPropertyChanged("ShortDescription")
            End Set
        End Property

        Private _ShortDescription As String
        Public ReadOnly Property ShortDescription As String
            Get
                Return _ShortDescription
            End Get
        End Property

        Public Property Length As TimeSpan
            Get
                Return Model.Length
            End Get
            Set(ByVal value As TimeSpan)
                Model.Length = value
                OnPropertyChanged("Length")
            End Set
        End Property

        Public Property FirstRetrieveDateTime As DateTime
            Get
                Return Model.FirstRetrieveDateTime
            End Get
            Set(ByVal value As DateTime)
                Model.FirstRetrieveDateTime = value
                OnPropertyChanged("FirstRetrieveDateTime")
            End Set
        End Property

        Public Property ThumbnailUri As Uri
            Get
                Return Model.ThumbnailUri
            End Get
            Set(ByVal value As Uri)
                Model.ThumbnailUri = value
                OnPropertyChanged("ThumbnailUri")
            End Set
        End Property

        Public Sub New(ByVal model As Video)
            Me.Model = model
        End Sub

        Public Function ToModel() As Video
            Return Model
        End Function

        Public Sub DownloadInfoAsync()
            Dim thread = New System.Threading.Thread(
                         AddressOf DownloadInfo)
            thread.Start()
        End Sub

        Private Sub DownloadInfo()
            Try
                Dim doc = XDocument.Load("http://ext.nicovideo.jp/api/getthumbinfo/" & Id)
                Title = doc.<nicovideo_thumb_response>.<thumb>.<title>.Value
                Description = doc.<nicovideo_thumb_response>.<thumb>.<description>.Value
                ThumbnailUri = New Uri(doc.<nicovideo_thumb_response>.<thumb>.<thumbnail_url>.Value)

                Dim l = doc.<nicovideo_thumb_response>.<thumb>.<length>.Value
                Dim i = l.IndexOf(":")
                Dim m = l.Substring(0, i)
                Dim s = l.Substring(i + 1)
                Length = New TimeSpan(0, System.Convert.ToInt32(m), System.Convert.ToInt32(s))

                FirstRetrieveDateTime =
                    DateTime.Parse(doc.<nicovideo_thumb_response>.<thumb>.<first_retrieve>.Value)

                OnDownloadInfoCompleted(EventArgs.Empty)
            Catch ex As Exception
#If DEBUG Then
                Stop
#End If
            End Try


        End Sub
    End Class
End Namespace
